package com.leruan.aasimp.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.bean.UsedCarBuyMainBean;
import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.dao.UsedCarBuyDao;
import com.leruan.aasimp.entity.UsedCarBuy;

public class UsedCarBuyDaoImpl extends HibernateDaoSupport implements
		UsedCarBuyDao {

	public List<UsedCarBuyMainBean> getUsedCarBuyList(Map<String, Object> map,
			int pageNo) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer(
				"select id, title, address, releaseTime from UsedCarBuy where 1 = 1 ");

		if (map.get("address") != null)
			sb.append(" and address=:address ");
		if (map.get("price") != null)
			sb.append(" and money=:price ");
		sb.append("order by releaseTime desc");
		Query query = session.createQuery(sb.toString());

		if (map.get("address") != null)
			query.setString("address", map.get("address") + "");
		if (map.get("price") != null)
			query.setInteger("price", (Integer) map.get("price"));

		int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE_2;
		query.setFirstResult(first);
		query.setMaxResults(UsedCarConstant.PAGE_SIZE_2);

		List list = query.list();

		ArrayList<UsedCarBuyMainBean> usedCarList = new ArrayList<UsedCarBuyMainBean>();

		for (int i = 0; i < list.size(); i++) {
			UsedCarBuyMainBean uBean = new UsedCarBuyMainBean();
			Object[] object = (Object[]) list.get(i);
			uBean.setId(Long.parseLong(object[0].toString()));
			uBean.setTitle((String) object[1]);
			uBean.setAddress((String) object[2]);
			uBean.setReleaseTime(((Date) object[3]));
			usedCarList.add(uBean);
		}
		return usedCarList;
	}

	public Long getUsedBuyTotal(Map<String, Object> map) throws Exception {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer(
				"select count(id) from UsedCarBuy where 1 = 1 ");
		if (map.get("address") != null)
			sb.append(" and address=:address ");
		if (map.get("price") != null)
			sb.append(" and money=:price ");
		Query query = session.createQuery(sb.toString());

		if (map.get("address") != null)
			query.setString("address", map.get("address") + "");
		if (map.get("price") != null)
			query.setInteger("price", (Integer) map.get("price"));

		return (Long) query.uniqueResult();
	}

	public UsedCarBuy getUsedCarBuyById(String id) throws Exception {
		Session session = this.getSession();
		String hql = "from UsedCarBuy where id = ?";
		return (UsedCarBuy) session.createQuery(hql).setString(0, id)
				.uniqueResult();
	}

	public List<UsedCarBuyMainBean> getUsedCarBuyListById(String userId)
			throws Exception {
		Session session = this.getSession();
		String hql = "select id, title, address, releaseTime from UsedCarBuy where userId = ? order by releaseTime desc";
		
		Query query = session.createQuery(hql);
		
		List list = query.setString(0, userId).list();

		ArrayList<UsedCarBuyMainBean> usedCarList = new ArrayList<UsedCarBuyMainBean>();

		for (int i = 0; i < list.size(); i++) {
			UsedCarBuyMainBean uBean = new UsedCarBuyMainBean();
			Object[] object = (Object[]) list.get(i);
			uBean.setId(Long.parseLong(object[0].toString()));
			uBean.setTitle((String) object[1]);
			uBean.setAddress((String) object[2]);
			uBean.setReleaseTime(((Date) object[3]));
			usedCarList.add(uBean);
		}
		return usedCarList;
	}

	public void deleteMyUsedCarBuy(String id, String userId) throws Exception {
		Session session = this.getSession();
		String hql = "delete UsedCarBuy where id = ? and userId = ? ";
		session.createQuery(hql).setString(0, id).setString(1, userId).executeUpdate();
	}

	public void saveUsedCarBuy(Map<String, String> map) throws Exception {
		Session session = this.getSession();
		UsedCarBuy usedCarBuy = new UsedCarBuy();
		
		usedCarBuy.setTitle(map.get("title"));
		usedCarBuy.setGearbox(Long.parseLong((map.get("gearbox"))));
		usedCarBuy.setDispalcementSize(Long.parseLong(map.get("dispalcementSize")));
		usedCarBuy.setMileageSize(Long.parseLong(map.get("mileageSize")));
		usedCarBuy.setMoney(Long.parseLong(map.get("money")));
		usedCarBuy.setAddress(map.get("address"));
		usedCarBuy.setContent(map.get("content"));
		usedCarBuy.setTelephone(Long.parseLong(map.get("telephone")));
		usedCarBuy.setName(map.get("name"));
		usedCarBuy.setStatus(Long.parseLong("1"));
		usedCarBuy.setBrowseNum(Long.parseLong("0"));
		usedCarBuy.setReleaseTime(new Date());
		usedCarBuy.setUserId(map.get("userId"));
		
		session.save(usedCarBuy);
	}

	public void updateUsedCarBuy(UsedCarBuy usedCarBuy) {
		Session session = this.getSession();
		session.update(usedCarBuy);		
	}

}
